/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smarttmt.idu.web.beans;

import com.smarttmt.idu.ejb.support.EstadoCuentaWrapper;
import com.smarttmt.idu.web.behavior.Preloadable;
import com.smarttmt.idu.web.model.Chip;
import com.smarttmt.idu.web.persistence.controller.VwEstadocuentaValorizaJpaController;
import com.smarttmt.idu.web.persistence.entity.VwEstadocuentaValoriza;
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceUnit;
import javax.transaction.UserTransaction;

/**
 *
 * @author Nicolás Rubén Caballero Ortega (helaman58@gmail.com)
 */
@Named(value = "customerOperationsBean")
@SessionScoped
public class CustomerOperations implements Serializable, Preloadable {

    //private java.util.List<CustomerBalance> customerBalanceList;
    private java.util.List<EstadoCuentaWrapper> customerBalanceList;
    private VwEstadocuentaValorizaJpaController vwEstadocuentaValorizaJpaController;
    private java.util.List<com.smarttmt.idu.web.persistence.entity.VwEstadocuentaValoriza> vwEstadocuentaValorizaList;
    private Integer numberElements;
    private Chip chipAux;
    private java.util.List<Chip> chipListRegistered;
    @PersistenceUnit(unitName = "smarttmtiduappPU")
    EntityManagerFactory emf;
    @Resource
    UserTransaction utx;

    /**
     * Creates a new instance of customerOperations
     */
    public CustomerOperations() {
    }

    @PostConstruct
    public void initPostConstructorCustomerOperations() {
        vwEstadocuentaValorizaJpaController = new VwEstadocuentaValorizaJpaController(utx, emf);
    }

    public void resetData() {
        this.customerBalanceList = null;
        this.numberElements = 0;
        this.chipAux = new Chip();
        this.vwEstadocuentaValorizaList = null;
    }

    public boolean validateIfContains() {
        if (chipListRegistered.isEmpty()) {
            return true;
        }

        Chip auxM;
        java.util.Iterator<Chip> it = chipListRegistered.iterator();
        while (it.hasNext()) {
            auxM = it.next();
            if (auxM.getChipNumber().equals(chipAux.getChipNumber())) {
                return false;
            }
        }

        return true;
    }

    public void chipVerified() {
        if (this.chipListRegistered == null) {
            this.chipListRegistered = new java.util.ArrayList<>();
        }

        if (validateIfContains()) {

            if (chipAux != null) {
                chipListRegistered.add(chipAux);
            }
        }
    }

    public void getChipInformation() {
        if (vwEstadocuentaValorizaList == null) {
            vwEstadocuentaValorizaList = new java.util.ArrayList<>();
        }
        if (customerBalanceList == null) {
            customerBalanceList = new java.util.ArrayList<>();
        }

        vwEstadocuentaValorizaList = vwEstadocuentaValorizaJpaController.findVwEstadocuentaValorizaByChip(chipAux.getChipNumber());

        java.util.Iterator<VwEstadocuentaValoriza> it = vwEstadocuentaValorizaList.iterator();
        VwEstadocuentaValoriza aux;
        while (it.hasNext()) {
            aux = it.next();
            customerBalanceList.add(vwEstadocuentaValorizaJpaController.fromVwEstadocuentaValorizaToEstadoCuentaWrapper(aux));
        }
        
        numberElements = customerBalanceList.size();
    }

    @Override
    public void preload(Map<String, Object> parameters) throws Exception {
        resetData();
        chipAux = (Chip) parameters.get("chip");
        
        if(chipAux.getInmobAddressNumber()==null || chipAux.getInmobRegistryNumber()==null){
            
        }
        
        if (chipAux != null) {
            getChipInformation();
            chipVerified();
        }
    }

    public List<EstadoCuentaWrapper> getCustomerBalanceList() {
        return customerBalanceList;
    }

    public void setCustomerBalanceList(List<EstadoCuentaWrapper> customerBalanceList) {
        this.customerBalanceList = customerBalanceList;
    }

    /*
     public List<CustomerBalance> getCustomerBalanceList() {
     return customerBalanceList;
     }

     public void setCustomerBalanceList(List<CustomerBalance> customerBalanceList) {
     this.customerBalanceList = customerBalanceList;
     }
     */
    public Integer getNumberElements() {
        return numberElements;
    }

    public void setNumberElements(Integer numberElements) {
        this.numberElements = numberElements;
    }

    public Chip getChipAux() {
        return chipAux;
    }

    public void setChipAux(Chip chipAux) {
        this.chipAux = chipAux;
    }

    public List<Chip> getChipListRegistered() {
        return chipListRegistered;
    }

    public void setChipListRegistered(List<Chip> chipListRegistered) {
        this.chipListRegistered = chipListRegistered;
    }

    public VwEstadocuentaValorizaJpaController getVwEstadocuentaValorizaJpaController() {
        return vwEstadocuentaValorizaJpaController;
    }

    public void setVwEstadocuentaValorizaJpaController(VwEstadocuentaValorizaJpaController vwEstadocuentaValorizaJpaController) {
        this.vwEstadocuentaValorizaJpaController = vwEstadocuentaValorizaJpaController;
    }

    public EntityManagerFactory getEmf() {
        return emf;
    }

    public void setEmf(EntityManagerFactory emf) {
        this.emf = emf;
    }

    public UserTransaction getUtx() {
        return utx;
    }

    public void setUtx(UserTransaction utx) {
        this.utx = utx;
    }

    public List<VwEstadocuentaValoriza> getVwEstadocuentaValorizaList() {
        return vwEstadocuentaValorizaList;
    }

    public void setVwEstadocuentaValorizaList(List<VwEstadocuentaValoriza> vwEstadocuentaValorizaList) {
        this.vwEstadocuentaValorizaList = vwEstadocuentaValorizaList;
    }

}
